package controllers

import (
	"encoding/json"
	"time"

	"gitee.com/lzqysoft/ims/models"

	"github.com/astaxie/beego"
)

type ImplementDetailController struct {
	CommonController
}

func (c *ImplementDetailController) URLMapping() {
	c.Mapping("GetImplementDetailsByImplement", c.GetImplementDetailsByImplement)
	c.Mapping("DebugImplementDetail", c.DebugImplementDetail)
	c.Mapping("InsatallImplementDetail", c.InsatallImplementDetail)
}

// GetImplementDetailsByImplement ...
// @Title GetImplementDetailsByImplement
// @Description GetImplementDetailsByImplement
// @Param	implementId		path 	string	true		"The id you want to GetImplementDetailsByImplement"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /GetImplementDetailsByImplement [post]
func (c *ImplementDetailController) GetImplementDetailsByImplement() {
	//user := c.User
	sdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrInputData)
	}
	var form QueryParams
	if err = json.Unmarshal(sdata, &form); err != nil {
		c.RetError(ErrInputData)
	}
	beego.Debug(form)
	impdetails, err := models.GetAllImplements(form.Query, form.Fields, form.SortBy, form.Order, (form.Page-1)*form.PageSize, form.PageSize)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	implementId , ok := form.Query["ImplementId"]
	if !ok {
		c.RetError(ErrInputData)
	}
	count, err := models.GetImplementDetailsCountByImplementId(implementId)

	res := make(map[string]interface{})
	res["list"] = impdetails
	res["total"] = count
	c.RetSuccess(res)
}

// DebugImplementDetail ...
// @Title DebugImplementDetail
// @Description DebugImplementDetail安装前调试
// @Param	ImplementDetail		path 	models.Implement	true		"The id you want to DebugImplementDetail"
// @Success 200 {models.Implement} debug success!
// @Failure 403 id is empty
// @router /DebugImplementDetail [post]
func (c *ImplementDetailController) DebugImplementDetail() {
	user := c.User
	data, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	var item models.ImplementDetail
	if err = json.Unmarshal(data, &item); err != nil {
		c.RetError(ErrInputData)
	}
	//item.InstallResult = 1
	item.Status = 2
	item.SetTime = time.Now()
	item.Seter = user.Abbr
	item.Utime = time.Now()
	if err = models.UpdateImplementDetail(&item); err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	c.RetSuccess("")

}

// InsatallImplementDetail ...
// @Title InsatallImplementDetail
// @Description InsatallImplementDetail
// @Param	InsatallImplementDetail		path 	models.Implement	true		"The id you want to DebugImplementDetail"
// @Success 200 {models.InsatallImplementDetail} debug success!
// @Failure 403 id is empty
// @router /InsatallImplementDetail [post]
func (c *ImplementDetailController) InsatallImplementDetail() {
	user := c.User
	data, err := c.AesDecodeToByte(c.GetString("tdata"))
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}
	var item models.ImplementDetail
	if err = json.Unmarshal(data, &item); err != nil {
		c.RetError(ErrInputData)
	}
	item.Installer = user.Abbr
	item.InstallTime = time.Now()
	//if err = models.UpdateImplementDetail(&item); err != nil {
	//	beego.Debug(err)
	//	c.RetError(ErrInputData)
	//}
	err = models.InstallSync(&item, user)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	c.RetSuccess("")
}
